/*
 * @Author: hongbin
 * @Date: 2022-11-21 10:26:47
 * @LastEditors: hongbin
 * @LastEditTime: 2022-12-08 13:22:34
 * @Description:
 */
import { ObjectId } from "mongodb";
import { NextApiRequest, NextApiResponse } from "next";
import { VerifyFields } from "../../../src/api/utils";
import DB from "../mongodb";
import { receiveRequest } from "../utils";

export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
    if (!receiveRequest(req, res, "POST")) return;
    // 通过socket发送申请加好友消息同意后双方均执行此请求
    console.log("请求内容", req.body);
    if (!VerifyFields.has(req.body, ["friendsList"])) {
        return res.status(500).json("缺少关键字段");
    }
    const { friendsList } = req.body;
    const db = await DB;

    const ids = friendsList.map((info: { data: number; friendId: string }) => new ObjectId(info.friendId));
    console.log("好友id:", ids);

    const friendsInfoList = [];
    for (let i = 0; i < ids.length; i++) {
        const id = ids[i];
        const info = await db.collection("userInfo").findOne({ _id: id });
        friendsInfoList.push(info);
    }

    console.log("好友信息:", friendsInfoList);
    res.json({ data: friendsInfoList });
}
